<?php
    Class Solution {
        /**
         * 387. 字符串中的第一个唯一字符
         * @param String $s
         * @return Integer
         */
        function firstUniqChar387($s) {
            $map  = [];
            $map2 = [];
            for ($i = 0; $i < strlen($s); $i++) {
                if(!isset($map[$s[$i]]) && !isset($map2[$s[$i]])) {
                    $map[$s[$i]] = $i;
                }else{
                    unset($map[$s[$i]]);
                    $map2[$s[$i]] = $s[$i];
                }
            }
            if(!$map){
                return -1;
            }

            return reset($map);
        }

        /**
         * 389. 找不同
         * @param String $s
         * @param String $t
         * @return String
         */
        function findTheDifference389($s, $t) {
            $map = [];
            for ($i = 0; $i < strlen($s); $i++) {
                if(!isset($map[$s[$i]])){
                    $map[$s[$i]] = 1;
                }else {
                    $map[$s[$i]]++;
                }
            }
            for($i = 0; $i < strlen($t); $i++) {
                if(!$map[$t[$i]]){
                    return $t[$i];
                }else{
                    $map[$t[$i]]--;
                }
            }

            return 0;
        }

        /**
         * 392. 判断子序列
         * @param String $s
         * @param String $t
         * @return Boolean
         */
        function isSubsequence392($s, $t) {
            if(!$s){
                return true;
            }
            $x = 0;
            $y = 0;
            while ($y < strlen($t)) {
                if($s[$x] == $t[$y]) {
                    $x++;
                    $y++;
                }else{
                    $y++;
                }
                if(!($s[$x])){
                    return true;
                }
            }

            return false;
        }

        /**
         * 405. 数字转换为十六进制数
         * @param Integer $num
         * @return String
         */
        function toHex405($num) {
            if($num == 0){
                return '0';
            }
            if($num < 0) {
                $num = 16**8+$num;
            }
            $s = '';
            $map = [
                0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4',
                5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9',
                10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e',
                15 => 'f',
            ];
            while($num > 0 ){
                $s   = $map[$num % 16] . $s;
                $num = intval($num / 16);
            }

            return $s;
        }

        /**
         * 409. 最长回文串
         * @param String $s
         * @return Integer
         */
        function longestPalindrome409($s) {
            $count  = 0;
            $map    = [];
            $single = [];
            for ($i = 0; $i < strlen($s); $i++) {
                if(!$map[$s[$i]]){
                    $map[$s[$i]] = 1;
                    if(!$single[$s[$i]]){
                        $single[$s[$i]] = 1;
                    }
                }else{
                    $map[$s[$i]] = 0;
                    $count += 2;
                    if($single[$s[$i]]){
                        unset($single[$s[$i]]);
                    }
                }
            }
            echo $count;
            if($single){
                $count++;
            }

            return $count;
        }

        /**
         * 412. Fizz Buzz
         * @param Integer $n
         * @return String[]
         */
        function fizzBuzz412($n) {
            $arr = [];
            for($i = 1; $i <= $n; $i++) {
                $s = '';
                if($i < 3){
                    $s = $s = strval($i);
                }else{
                    if($i % 3 == 0) {
                        $s = 'Fizz';
                    }
                    if($i >= 5){
                        if($i % 5 == 0) {
                            $s .= 'Buzz';
                        }
                    }
                    if(!$s){
                        $s = strval($i);
                    }
                }
                $arr[] = $s;
            }

            return $arr;
        }

        /**
         *414. 第三大的数
         * @param Integer[] $nums
         * @return Integer
         */
        function thirdMax414($nums) {
            $map[1] = null;
            $map[2] = null;
            $map[3] = null;

            foreach ($nums as $num) {
                if ($num === $map[1] || $num === $map[2] || $num === $map[3]) {
                    continue;
                }
                if ($map[1] === null || $num > $map[1]) {
                    $map[3] = $map[2];
                    $map[2] = $map[1];
                    $map[1] = $num;
                } elseif ($map[2] === null || $num > $map[2]) {
                    $map[3] = $map[2];
                    $map[2] = $num;
                } elseif ($map[3] === null || $num > $map[3]) {
                    $map[3] = $num;
                }
            }

            return $map[3] !== null ?  $map[3] : $map[1];
        }

        /**
         *415. 字符串相加
         * @param String $num1
         * @param String $num2
         * @return String
         */
        function addStrings415($num1, $num2) {
            $i = strlen($num1)-1;
            $j = strlen($num2)-1;
            $ans = 0;
            $total = '';
            while($i >= 0 || $j >= 0 || $ans > 0) {
                $plus1 = ($i >= 0) ? $num1[$i] : 0;
                $plus2 = ($j >= 0) ? $num2[$j] : 0;
                $sum   = $plus1 + $plus2 + $ans;
                $ans   = ($sum >= 10) ? 1 : 0;
                $total = ($sum % 10) . $total;
                $i--;
                $j--;
            }

            return $total;
        }

        /**
         * 434. 字符串中的单词数
         * @param String $s
         * @return Integer
         */
        function countSegments434($s) {
            $count = 0;
            $yes   = 0;
            for($i = 0; $i < strlen($s); $i++) {
                if(!$yes){
                    if($s[$i] && $s[$i] != ' '){
                        $yes = 1;
                    }
                }
                if($yes && ($s[$i+1] == ' ' || !$s[$i+1])){
                    $count++;
                    $yes = 0;
                }
            }

            return $count;
        }

        /**
         * 441. 排列硬币
         * @param Integer $n
         * @return Integer
         */
        function arrangeCoins441($n) {
            $n = sqrt(2*$n+0.25)-0.5;

            return intval($n);
        }


    }

    $solution = new Solution();
    $n = [1,2,2,5,3,5];
    print_r($solution->thirdMax414($n));

?>